package com.baixiaowen.xiaoaointerview.大厂笔试_白板面试篇.大文件词频统计;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class BufferExample2 {

    public static void main(String[] args) throws IOException {
        compare_with_single();
    }

    /**
     * 单线程中文词频统计
     * @throws IOException
     */
    public static void compare_with_single() throws IOException {

        BufferedInputStream in = new BufferedInputStream(new FileInputStream("word.txt"));
        byte[] buf = new byte[4*1024];
        int len = 0;
        Map<String, Integer> total = new HashMap<>();
        long startTime = System.currentTimeMillis();
        while ((len = in.read(buf)) != -1) {
            byte[] bytes = Arrays.copyOfRange(buf, 0, len);
            String str = new String(bytes);
            Map<String, Integer> hashMap = countByString(str);
            for(Map.Entry<String, Integer> entry : hashMap.entrySet()) {
                String key = entry.getKey();
                incKey(key, total, entry.getValue());
            }
        }

        System.err.println("time:" + (System.currentTimeMillis() - startTime) + "ms");
        System.err.println(total.get("ab"));
        System.err.println(total.size());
    }

    private static Map<String, Integer> countByString(String str) {
        Map<String, Integer> map = new HashMap<>();
        StringTokenizer tokenizer = new StringTokenizer(str);
        while (tokenizer.hasMoreTokens()) {
            String word = tokenizer.nextToken();
            incKey(word, map, 1);
        }
        return map;
    }

    private static void incKey(String key, Map<String, Integer> map, int n) {
        if (map.containsKey(key)) {
            map.put(key, map.get(key) + 1);
        } else {
            map.put(key, n);
        }
    }

}
